package com.zmm.myyimixian.ui.activity.login;

import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.db.MyUser;
import com.zmm.myyimixian.util.TextUtils;
import com.zmm.myyimixian.util.UIManager;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class BoundActivity extends BaseActivity {

    @BindView(R.id.bound_toolbar)
    Toolbar boundToolbar;//toolBar
    @BindView(R.id.bound_et_name)
    TextInputEditText boundEtName;//用户名 手机号
    @BindView(R.id.bound_et_pass)
    TextInputEditText boundEtPass;//密码
    private MyUser myUser;

    @Override
    protected void initView() {
        Intent intent = getIntent();
        myUser = (MyUser) intent.getSerializableExtra("user");
        boundToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bound;
    }
    @OnClick({R.id.bound_btn})
    public void onClick(View view) {
        if (view.getId() == R.id.bound_btn) {
            boundUser();//绑定
        }
    }

    private void boundUser() {
        //判断输入的数据是否有空字符
        if (TextUtils.isNull(boundEtName) || TextUtils.isNull(boundEtPass)) {
            boundEtName.setError("用户名或密码不能为空");
            return;
        }
        final String phone = boundEtName.getText().toString();
        final String pass = boundEtPass.getText().toString();
        if (TextUtils.isMobileNO(phone)) {//验证输入的是否是手机号
            //验证密码长度和是否包含中文
            if (pass.length() >= 6 && pass.length() <= 16 && !TextUtils.isContainsChinese(pass)) {
                // 使用BmobSDK提供的注册功能
                myUser.setUsername(phone);
                myUser.setPassword(pass);
                myUser.setPass(pass);
                myUser.signUp(BoundActivity.this, new SaveListener() {
                    @Override
                    public void onSuccess() {
                        login(phone, pass);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                       //注册失败，说明已经存在该手机号，现在需要将qq登陆的信息绑定到手机号中
                        login(phone, pass);
                    }
                });
            }else {
                boundEtPass.setError("密码格式错误请重新输入");
            }

        }else {
            boundEtName.setError("手机格式错误,请重新输入");
        }
    }

    private void login(String phone, String pass) {
        //1.先登录该账号
        BmobUser.loginByAccount(BoundActivity.this, phone, pass, new LogInListener<MyUser>() {
            @Override
            public void done(MyUser user, BmobException e) {
                if (user != null) {
                    //登陆成功后 将qq信息更新到Bmob中
                    BmobUser bmobUser = BmobUser.getCurrentUser(BoundActivity.this);
                    myUser.update(BoundActivity.this, bmobUser.getObjectId(), new UpdateListener() {
                        @Override
                        public void onSuccess() {
                            Toast.makeText(BoundActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                            //保存成功
                            saveUser();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            //保存失败
                            Toast.makeText(BoundActivity.this, "网络异常！", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    Toast.makeText(BoundActivity.this, "手机号已存在，密码错误！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //保存用户信息并跳转到主页面
    private void saveUser() {
        //保存用户信息
        MyApp.setUserPhone(myUser.getUsername());
        MyApp.setNickname(myUser.getNick());
        MyApp.setGender(myUser.getGender());
        MyApp.setFigureurl(myUser.getFigureurl());
        MyApp.login();//登陆成功
        UIManager.startMain(BoundActivity.this);
    }
}
